package com.leyou.item.controller;

import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父id查询子节点
     * @param pid
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoriesByPid
    (@RequestParam(value = "pid" ,defaultValue = "0 ") Long pid){
        if(pid == null || pid.longValue() < 0){
            //响应400,参数不合法相当于ResponseEnttity.status(HttpStatus.BAD_REQUEST).build();
            return ResponseEntity.badRequest().build();
        }
        List<Category> categories = this.categoryService.queryCategoriesByPid(pid);
        if (CollectionUtils.isEmpty(categories)){

            //404: 资源服务器未找到
            return ResponseEntity.notFound().build();
        }
            //200: 查询成功
        return ResponseEntity.ok(categories);
    }

    /**
     * 回显产品品牌
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid")Long bid){
        List<Category> list = this.categoryService.queryByBrandId(bid);
        if(list == null || list.size() < 1){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);

        }

        return ResponseEntity.ok(list);
    }

    /**
     * 根据分类id查询分类名称
     * @param ids
     * @return
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryCategoryNameByCid(
            @RequestParam("ids") List<Long> ids){
        List<String> categoryName = this.categoryService.queryNameById(ids);

        if(categoryName == null || CollectionUtils.isEmpty(categoryName)){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(categoryName);
    }




}
